package com.chisheng.caa.controller;

import com.chisheng.caa.pojo.ImportCourseSetting;
import com.chisheng.caa.pojo.response.JsonResponse;
import com.chisheng.caa.pojo.dto.CourseDTO;
import com.chisheng.caa.pojo.dto.ExcelSheetDTO;
import com.chisheng.caa.pojo.dto.ImportCourseSettingDTO;
import com.chisheng.caa.pojo.entity.FileMetadata;
import com.chisheng.caa.service.ExcelImportCourseService;
import com.chisheng.caa.service.FileMetadataService;
import com.chisheng.caa.util.EasyExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@CrossOrigin
@Api("导课控制器")
@RestController
@RequestMapping(value = "/api/caa/importing", produces = MediaType.APPLICATION_JSON_VALUE)
public class ImportCourseController {

    @Resource
    private ExcelImportCourseService service;

    @Resource
    private FileMetadataService metadataService;

    @ApiOperation("打开导入服务")
    @PostMapping("/open")
    public JsonResponse<String> open() {
        return JsonResponse.success(service.open());
    }

    @ApiOperation("从 Excel 文件中读取课程")
    @PostMapping("/load/{accessKey}/{fileCode}")
    public JsonResponse<String> load(@ApiParam("访问键") @PathVariable String accessKey,
                                     @ApiParam("文件码") @PathVariable String fileCode) {
        FileMetadata metadata = metadataService.getById(fileCode);
        System.out.println(metadata);
        if (metadata == null || !EasyExcelUtil.isExcel(metadata)) {
            return JsonResponse.failure(String.format("无效文件码 '%s'", fileCode));
        }

        service.load(accessKey, metadata.getUrl());
        return JsonResponse.success();
    }

    @ApiOperation("获取工作表信息列表")
    @GetMapping("/sheet/list/{accessKey}")
    public JsonResponse<List<ExcelSheetDTO>> sheetList(@ApiParam("访问键") @PathVariable String accessKey) {
        return JsonResponse.success(service.getSheetList(accessKey));
    }

    @ApiOperation("切换 Excel 的工作表")
    @PostMapping("/sheet/switch/{accessKey}/{sheetNo}")
    public JsonResponse<?> switchSheet(@ApiParam("访问键") @PathVariable String accessKey,
                                       @ApiParam("表下标") @PathVariable Integer sheetNo) {
        service.switchSheet(accessKey, sheetNo);
        return JsonResponse.success();
    }

    @ApiOperation("添加课程")
    @PutMapping("/add/{accessKey}")
    public JsonResponse<?> add(@ApiParam("访问键") @PathVariable String accessKey,
                               @ApiParam("添加的课程信息") @Validated @RequestBody CourseDTO course) {
        service.add(accessKey, course);
        return JsonResponse.success();
    }

    @ApiOperation("移除课程")
    @DeleteMapping("/remove/{accessKey}/{courseCode}")
    public JsonResponse<?> remove(@ApiParam("访问键") @PathVariable String accessKey,
                                  @ApiParam("课程代码") @PathVariable String courseCode) {
        service.remove(accessKey, courseCode);
        return JsonResponse.success();
    }

    @ApiOperation("更新课程")
    @PostMapping("/update/{accessKey}")
    public JsonResponse<?> update(@ApiParam("访问键") @PathVariable String accessKey,
                                  @ApiParam("课程代码") @Validated @RequestBody CourseDTO course) {
        service.modify(accessKey, course);
        return JsonResponse.success();
    }

    @ApiOperation("获取课程列表")
    @GetMapping("/list/{accessKey}")
    public JsonResponse<Collection<CourseDTO>> list(@ApiParam("访问键") @PathVariable String accessKey) {
        return JsonResponse.success(service.getCollection(accessKey));
    }

    @ApiOperation("设定导课参数")
    @PostMapping("/setting/set/{accessKey}")
    public JsonResponse<?> setSetting(@ApiParam("访问键") @PathVariable String accessKey,
                                      @ApiParam("导入课程设定") @Validated
                                      @RequestBody ImportCourseSettingDTO setting) {
        service.setSetting(accessKey, setting);
        return JsonResponse.success();
    }

    @ApiOperation("获取导课参数")
    @GetMapping("/setting/get/{accessKey}")
    public JsonResponse<ImportCourseSetting> getSetting(@ApiParam("访问键") @PathVariable String accessKey) {
        return JsonResponse.success(service.getSetting(accessKey));
    }

    @ApiOperation("课程数据写入")
    @PostMapping("/write/{accessKey}")
    public JsonResponse<?> write(@ApiParam("访问键") @PathVariable String accessKey) {
        service.write(accessKey);
        return JsonResponse.success();
    }

}
